%%

%byaccj

%{
  private Parser yyparser;

  public Yylex(java.io.Reader r, Parser yyparser) {
    this(r);
    this.yyparser = yyparser;
  }
%}
comment = \/\/([a-zA-Z]|[0-9]|" "|:|=)*\/\/
space = ([\t] | " ")*

Integer = [0-9]+
Double = [0-9]+("."[0-9]+)?
Bool = "TRUE" | "FALSE"

string = \"([a-zA-Z]|[0-9]|" "|:|=)*\"

typeInt = "Integer"
typeDouble = "Double"
typeBool = "Bool"

Write = "write"
WriteLine = "writeLine"
Definition = "define"
If = "if"
Then = "then"
Else = "else"
Endif = "endif"

While = "while"
Do = "do"
Endwhile = "endwhile"

Plus = "+"
Minus = "-"
Multiply = "*"
Divide = "/"
Exp = "^"
OpenPar = "("
ClosePar = ")"
And = "&"
Or = "|"
Equal = "="
Not = "!"
LessThan = "<"
LessEqual = "<="
Greater = ">"
GreaterEqual = ">="

Id = [a-z]([a-z]|[A-Z]|[0-9])*

SC  = ";"
NewLine = \n | \r | \r\n | \t

%%
{comment} {/*Do nothing*/}

{NewLine} {/*skip new lines*/}

/* semicolon */
{SC}   { return Parser.SC; }

{space}  { yyparser.yylval = new ParserVal(yytext()); return Parser.space; }

{Definition}  { yyparser.yylval = new ParserVal(yytext()); return Parser.Definition; }

{Integer}  { yyparser.yylval = new ParserVal(Integer.parseInt(yytext())); return Parser.Integer; }

{Double}  { yyparser.yylval = new ParserVal(Double.parseDouble(yytext())); return Parser.Double; }

{Bool}  { yyparser.yylval = new ParserVal(yytext().equals("TRUE") ? 1 : 0); return Parser.Bool; }

/* text */
{string}  { String ret = yytext(); ret = ret.substring(1, ret.length() - 1); yyparser.yylval = new ParserVal(ret); return Parser.string; }

/* datatype integer */
{typeInt} { yyparser.yylval = new ParserVal(yytext()); return Parser.typeInt; }

/* datatype double */
{typeDouble} { yyparser.yylval = new ParserVal(yytext()); return Parser.typeDouble; }

/* datatype boolean */
{typeBool} { yyparser.yylval = new ParserVal(yytext()); return Parser.typeBool; }

{If} { yyparser.yylval = new ParserVal(yytext()); return Parser.If; }

{Then} { yyparser.yylval = new ParserVal(yytext()); return Parser.Then; }

{Else} { yyparser.yylval = new ParserVal(yytext()); return Parser.Else; }

{Endif} { yyparser.yylval = new ParserVal(yytext()); return Parser.Endif; }

{While} { yyparser.yylval = new ParserVal(yytext()); return Parser.While; }

{Do} { yyparser.yylval = new ParserVal(yytext()); return Parser.Do; }

{Endwhile} { yyparser.yylval = new ParserVal(yytext()); return Parser.Endwhile; }

/* prints on the standard output*/
{Write} { yyparser.yylval = new ParserVal(yytext()); return Parser.Write; }

/* prints a new line on the standard output */
{WriteLine} { yyparser.yylval = new ParserVal(yytext()); return Parser.WriteLine; }

{Plus} { yyparser.yylval = new ParserVal(yytext()); return Parser.Plus; }

{Minus} { yyparser.yylval = new ParserVal(yytext()); return Parser.Minus; }

{Multiply} { yyparser.yylval = new ParserVal(yytext()); return Parser.Multiply; }

{Divide} { yyparser.yylval = new ParserVal(yytext()); return Parser.Divide; }

/* exponential */
{Exp} { yyparser.yylval = new ParserVal(yytext()); return Parser.Exp; }

/* open brackets */
{OpenPar} { yyparser.yylval = new ParserVal(yytext()); return Parser.OpenPar; }

/* closed brackets */
{ClosePar} { yyparser.yylval = new ParserVal(yytext()); return Parser.ClosePar; }

{And} { yyparser.yylval = new ParserVal(yytext()); return Parser.And; }

{Or} { yyparser.yylval = new ParserVal(yytext()); return Parser.Or; }

{Equal} { yyparser.yylval = new ParserVal(yytext()); return Parser.Equal; }

{Not} { yyparser.yylval = new ParserVal(yytext()); return Parser.Not; }

{LessThan} { yyparser.yylval = new ParserVal(yytext()); return Parser.LessThan; }

{LessEqual} { yyparser.yylval = new ParserVal(yytext()); return Parser.LessEqual; }

{Greater} { yyparser.yylval = new ParserVal(yytext()); return Parser.Greater; }

{GreaterEqual} { yyparser.yylval = new ParserVal(yytext()); return Parser.GreaterEqual; }

/* identifier */
{Id}  { yyparser.yylval = new ParserVal(yytext()); return Parser.Id; }

/* everything else is an error! */
[^]    { System.err.println("Error: unexpected character '"+yytext()+"'"); return -1; }
